﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;

namespace CSSoft.VNTower.Domain
{
    [Serializable]
    public class UserTaskItem : BaseItem
    {
        public ProjectItem Project { get; set; }
        public string ProjectCode { get { return Project.Title; } }
        public bool Delay { get; set; }
        public string ReasonDelay { get; set; }
        public decimal PercentComplete { get; set; }
        public DateTime? FS { get; set; }
        public DateTime? FF { get; set; }
        public DateTime? AS { get; set; }
        public DateTime? AF { get; set; }
        public string PMAproved { get; set; }
        public string PMComment { get; set; }
        public string ActionRegister { get; set; }
        public DateTime? PlannedFinish { get; set; }
        public string WorkingPlace { get; set; }
        public string Note { get; set; }
        public BaseItem SourceTask { get; set; }

        public bool PMAprovedCheckBox { get { return PMAproved == "Aproved"; } }
        public bool IsReporter { get; set; }
        public bool NotIsReporter { get { return !IsReporter; } }
        public bool IsProjectManager { get; set; }
        public bool NotIsProjectManager { get { return !IsProjectManager; } }

        public UserTaskItem ReadItem(SPListItem item)
        {
            this.Item = item;
            Delay = CS2Convert.ToBoolean(item["Delay"]);
            ReasonDelay = CS2Convert.ToString(item["ReasonDelay"]);
            PercentComplete = CS2Convert.ToDecimal(item["PercentComplete"]) * 100;
            FS = CS2Convert.ToDateTime(item["Start"]);
            FF = CS2Convert.ToDateTime(item["Finish"]);
            AS = CS2Convert.ToDateTime(item["ActualStart"]);
            AF = CS2Convert.ToDateTime(item["ActualFinish"]);
            PMAproved = CS2Convert.ToString(item["ManagerApproved"]);
            PMComment = CS2Convert.ToString(item["ManagerJudgerment"]);
            ActionRegister = CS2Convert.ToString(item["ActionRegister"]);
            PlannedFinish = CS2Convert.ToDateTime(item["PlannedFinish"]);
            WorkingPlace = CS2Convert.ToString(item["WorkingPlace"]);
            Note = CS2Convert.ToString(item["Note"]);
            Project = new ProjectItem { LookupValue = CS2Convert.ToLookupValue(item["Project"]) };
            SourceTask = new BaseItem { LookupValue = CS2Convert.ToLookupValue(item["SourceTask"]) };

            try
            {
                SPFieldUserValue reporter = CS2Convert.ToSPFieldUserValue(item["Author"]);
                IsReporter = reporter.User.ID == CS2Web.CurrentUser.ID;
            }
            catch { IsReporter = false; }
            try
            {
                SPList projectList = CS2Web.GetList(Constants.LIST_PROJECTS);
                SPQuery query = new SPQuery();
                query.Query = String.Format(@"<Where>
                      <And>
                         <Eq>
                            <FieldRef Name='ID' />
                            <Value Type='Counter'>{0}</Value>
                         </Eq>
                         <Eq>
                            <FieldRef Name='Manager' LookupId='TRUE' />
                            <Value Type='User'>{1}</Value>
                         </Eq>
                      </And>
                   </Where>", Project.Id, CS2Web.CurrentUser.ID);
                IsProjectManager = CS2SPQueryBuilder.HasItems(projectList.GetItems(query));
            }
            catch { IsProjectManager = false; }

            return this;
        }

        public UserTaskItem ReadTask(SPListItem item)
        {
            Id = item.ID * -1;
            Title = item.Title;
            Delay = false;
            PercentComplete = 0;
            FS = CS2Convert.ToDateTime(item["Start"]);
            FF = CS2Convert.ToDateTime(item["Finish"]);
            AS = FS;
            AF = FF;
            PMAproved = "Rejected";
            Project = new ProjectItem { LookupValue = CS2Convert.ToLookupValue(item["Project"]) };
            SourceTask = new BaseItem { Item = item };

            IsReporter = true;

            try
            {
                SPList projectList = CS2Web.GetList(Constants.LIST_PROJECTS);
                SPQuery query = new SPQuery();
                query.Query = String.Format(@"<Where>
                      <And>
                         <Eq>
                            <FieldRef Name='ID' />
                            <Value Type='Counter'>{0}</Value>
                         </Eq>
                         <Eq>
                            <FieldRef Name='Manager' LookupId='TRUE' />
                            <Value Type='User'>{1}</Value>
                         </Eq>
                      </And>
                   </Where>", Project.Id, CS2Web.CurrentUser.ID);
                IsProjectManager = CS2SPQueryBuilder.HasItems(projectList.GetItems(query));
            }
            catch { IsProjectManager = false; }
            return this;
        }
    }
}
